package com.ganbo.rabbitmq.delay;

import com.ganbo.mq.common.utils.DateUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 基本消息发送
 *
 * @Author: ganbo
 * @Date: 2020/12/8 19:21
 */

@RestController
public class DlayProducerController {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/delayQueue")
    public String sendDirectMessage() {
        for (int i = 0; i < 10000; i++) {
            String messageData = "测试延迟队列消息!" + i;
            MessageProperties messageProperties = new MessageProperties();
            // 延迟的间隔时间，目标时刻减去当前时刻
            messageProperties.setHeader("x-delay", 10000000);
            Message message = new Message(messageData.getBytes(), messageProperties);

            // 不能在本地测试，必须发送消息到安装了插件的服务端
            rabbitTemplate.send(DelayQueueConfig.EXCHANGE_DELAY, DelayQueueConfig.ROUTING_KEY_DELAY, message);

            System.out.println(DateUtil.formateDateyyyy_MM_ddHHmmss(new Date()) + "发送成功====>" + messageData);
        }
        return "ok";
    }
}
